package inter.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.orders.service.TOrdersService;
import com.dingpeikeji.quickdeer.modules.realsche.service.FRealscheService;

import inter.authorization.annotation.Authorization;
import inter.model.ResultModel;
/**
 * 订单操作接口
 * @author zijuan
 *
 */
@Controller
@RequestMapping(value = "api")
public class TorderOperateController {
	@Autowired
	private FRealscheService fRealscheService;
	@Autowired
	private TOrdersService tOrdersService;
	
	/**
	 * 订单查询
	 * @param userid
	 * @param orders
	 * @return
	 */
	@RequestMapping(value = "orderQuery")
	@Authorization
	public ResponseEntity<ResultModel> orderQuery(@RequestParam String userid,TOrders orders) {
		orders.setOrderuserid(userid);
		List<TOrders> tOrderList =  fRealscheService.getOrdersList(orders);
		return new ResponseEntity<ResultModel>(ResultModel.ok(tOrderList), HttpStatus.OK);
	}
	


	/**
	 * 订单取消
	 * @param userid
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value="orderCancel", method = RequestMethod.GET)
	@Authorization
	public ResponseEntity<ResultModel> orderCancel(@RequestParam String userid,@RequestParam String orderId){
		TOrders orders = tOrdersService.get(orderId);
		String cancelid = userid; // 取消人ID
		String message = fRealscheService.cancelOrder(orders, cancelid);
		if(message == null){
			return new ResponseEntity<ResultModel>(ResultModel.ok("取消订单成功"), HttpStatus.OK);
		} else {
			return new ResponseEntity<ResultModel>(ResultModel.no("取消订单失败"), HttpStatus.OK);
		}
	}
	
	/**
	 * 订单确定
	 * @param out_trade_no
	 * @param trade_no
	 * @param buyer_id
	 * @return
	 */
	@RequestMapping(value = "orderConfirm")
	public ResponseEntity<ResultModel> orderConfirm(@RequestParam String orderId,@RequestParam String flowNo,@RequestParam String buyer_id) {
		fRealscheService.proConfirmPay(Integer.parseInt(orderId), 0D, flowNo, buyer_id, TOrders.PAY_WAY_ALIPAY);
		return new ResponseEntity<ResultModel>(ResultModel.ok("订单确定成功"), HttpStatus.OK);
	}

}
